﻿using Basic.FormModel.DB;
using Basic.FormRemoteModel.PhyFormInter.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FormDAL.Repository
{
    internal class PhysicalTableDAL : BasicDAL<DBPhysicalTable, long>, IPhysicalTableDAL
    {
        public PhysicalTableDAL ( IRepository<DBPhysicalTable> basicDAL ) : base(basicDAL, "physical.table")
        {
        }
        public void Add ( long formId, PhyFormTable[] table )
        {
            DateTime now = DateTime.Now;
            List<DBPhysicalTableColumn> cols = new List<DBPhysicalTableColumn>();
            DBPhysicalTable[] add = table.ConvertAll(a =>
            {
                DBPhysicalTable tab = new DBPhysicalTable
                {
                    AddTime = now,
                    FormId = formId,
                    TableType = a.TableType,
                    Id = IdentityHelper.CreateId(),
                    TableLabel = a.TableLabel,
                    TableName = a.TableName
                };
                a.Column.ForEach(c =>
                {
                    cols.Add(new DBPhysicalTableColumn
                    {
                        DataType = c.DataType,
                        FuncType = c.FuncType,
                        FormId = formId,
                        Id = IdentityHelper.CreateId(),
                        Label = c.Label,
                        Name = c.Name,
                        PhyTableId = tab.Id,
                        ColumnSet = c.ColumnSet
                    });
                });
                return tab;
            });
            ISqlQueue<DBPhysicalTable> queue = this._BasicDAL.BeginQueue();
            queue.Insert(add);
            queue.InsertBy(cols);
            _ = queue.Submit();
        }
        public void Set ( long formId, PhyFormTable[] table )
        {
            DateTime now = DateTime.Now;
            List<DBPhysicalTableColumn> cols = new List<DBPhysicalTableColumn>();
            DBPhysicalTable[] add = table.ConvertAll(a =>
            {
                DBPhysicalTable tab = new DBPhysicalTable
                {
                    AddTime = now,
                    FormId = formId,
                    TableType = a.TableType,
                    Id = IdentityHelper.CreateId(),
                    TableLabel = a.TableLabel,
                    TableName = a.TableName
                };
                a.Column.ForEach(c =>
                {
                    cols.Add(new DBPhysicalTableColumn
                    {
                        DataType = c.DataType,
                        FormId = formId,
                        FuncType = c.FuncType,
                        Id = IdentityHelper.CreateId(),
                        Label = c.Label,
                        Name = c.Name,
                        PhyTableId = tab.Id,
                        ColumnSet = c.ColumnSet
                    });
                });
                return tab;
            });
            ISqlQueue<DBPhysicalTable> queue = this._BasicDAL.BeginQueue();
            queue.Delete(a => a.FormId == formId);
            queue.DeleteBy<DBPhysicalTableColumn>(a => a.FormId == formId);
            queue.Insert(add);
            queue.InsertBy(cols);
            _ = queue.Submit();
        }
        public void Clear ( long formId )
        {
            ISqlQueue<DBPhysicalTable> queue = this._BasicDAL.BeginQueue();
            queue.Delete(a => a.FormId == formId);
            queue.DeleteBy<DBPhysicalTableColumn>(a => a.FormId == formId);
            _ = queue.Submit();
        }
    }
}
